package com.tycg.muyu.chat.service.impl;

import com.tycg.muyu.chat.mapper.UserMapper;
import com.tycg.muyu.chat.pojo.MUser;
import com.tycg.muyu.chat.service.UserService;
import com.tycg.muyu.commons.utils.DateUtils;
import com.tycg.muyu.commons.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

/**
 * Created by xiaowu on 2020/2/20
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private IdWorker idWorker;

    @Override
    public void add(MUser user) {

        try {
            user.setId(idWorker.nextId());
            user.setCreateTime(DateUtils.parseDate2String(new Date()));
            userMapper.insert(user);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询全部用户
     */
    @Override
    public List<MUser> findAll() {
        return userMapper.findAll();
    }

    /**
     * 根据id查询用户信息
     *
     * @param id
     */
    @Override
    public MUser findById(Long id) {
        return userMapper.findById(id);
    }

    /**
     * 根据用户id删除
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {

        userMapper.deleteById(id);
    }

    /**
     * 根据用户id更新信息
     *
     * @param user
     */
    @Override
    public void update(MUser user) {

        userMapper.update(user);
    }
}
